﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Amazon.SimpleEmail.Model;
using Amazon.SimpleEmail;
using RestSharp;
using System.Net.Mail;

namespace BatchSendMail
{
    public partial class SendMailUseMailGun : Form
    {
        public SendMailUseMailGun()
        {
            InitializeComponent();
        }

        private void btnVerify_Click(object sender, EventArgs e)
        {
            try
            {
                MailAddress m = new MailAddress(txtEmail.Text);
                // Khong kiem tra voi yahoo mail.
                //if (!m.Host.ToLower().Equals("yahoo.com"))
                //{
                    EmailVerifier verify = new EmailVerifier(true);
                    bool rs = verify.CheckExists(txtEmail.Text);
                    if (!rs)
                    {
                        MessageBox.Show("Fail !");
                    }
                    else
                    {
                        MessageBox.Show("OK !");
                    }
                //}    
            }
            catch (Exception)
            {
                MessageBox.Show("Fail !");
            }
                   
        }

        public IRestResponse GetValidate()
        {
            RestClient client = new RestClient();
            client.BaseUrl = "https://api.mailgun.net/v2";
            client.Authenticator =
                    new HttpBasicAuthenticator("api",
                                               "pubkey-8i0jlieuf4qizhh6ozcncmi9ig3swdt6");
            IRestRequest request = new RestRequest();
            request.Resource = "/address/validate";
            request.AddParameter("address", txtEmail.Text);
            return client.Execute(request);
        }

        public IRestResponse SendSimpleMessage()
        {
            RestClient client = new RestClient();
            client.BaseUrl = "https://api.mailgun.net/v2";
            client.Authenticator = new HttpBasicAuthenticator(
                "api", "key-86y-l3ol82vw4owzngkmvbuywdaakgx3");
            IRestRequest request = new RestRequest();
            request.AddParameter("domain",
                                "sandbox92916.mailgun.org", ParameterType.UrlSegment);
            request.Resource = "{domain}/messages";
            request.AddParameter("from", "Excited User <excited@samples.mailgun.org>");
            request.AddParameter("to", txtEmail.Text.Trim());
            request.AddParameter("subject", "Hello");
            request.AddParameter("text", "Testing some Mailgun awesomeness!");
            request.Method = Method.POST;        
            return client.Execute(request);
        }

        private void btnSendWithMailGun_Click(object sender, EventArgs e)
        {
            IRestResponse response = SendSimpleMessage();
        }
    }
}
